package com.lesso.yzh.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.yzh.sunflower.domain.repository.EmployeeRepository;
import com.lesso.yzh.sunflower.infra.assembler.EmployeeAssembler;
import com.lesso.yzh.sunflower.infra.mapper.EmployeeMapper;
import com.lesso.yzh.sunflower.model.Employee;
import com.lesso.yzh.sunflower.pojo.dto.EmployeeQueryDTO;
import com.lesso.yzh.sunflower.pojo.vo.EmployeeVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * EmployeeRepositoryImpl
 *
 * @author yzh
 * @since 2025-07-02
 */
@Repository
public class EmployeeRepositoryImpl implements EmployeeRepository {
    private final EmployeeMapper employeeMapper;

    public EmployeeRepositoryImpl(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public Integer save(Employee employee) {
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setDelStatus(0);
        employeeMapper.insert(employee);
        return Math.toIntExact(employee.getId());
    }
    @Override
    public Integer update(Employee employee)
    {
        employee.setUpdateTime(LocalDateTime.now());
        return employeeMapper.updateById(employee);
    }

    @Override
    public Page<EmployeeVO> getPageList(EmployeeQueryDTO dto) {
        Page<EmployeeVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<Employee> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.hasText(dto.getName()), "name", dto.getName());
        List<Employee> employees = employeeMapper.selectPage(page, wrapper);
        return page.setRecords(EmployeeAssembler.INSTANCE.entityListToVOList(employees));
    }

    @Override
    public Page<EmployeeVO> getCustomPageList(EmployeeQueryDTO dto) {
        Page<EmployeeVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<EmployeeVO> list = employeeMapper.selectPageList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public void removeById(Long id) {
        employeeMapper.deleteById(id);
    }
}
